package com.terry.test3;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * @author 小八
 * @date 2023年02月14日11:13
 */
class Node{
    public int index ;
    public char color;
    public int value ;

    public int getIndex(){
        return this.index;
    }
}
public class Title40 {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        Map<Node,Node> rsuMap = new HashMap<>();
        int[] arr = new int[]{1,4,3,4,3};
        char[] colorArr = new char[]{'r','y','b','b','r'};
        Node[] nodes = new Node[arr.length];
        for (int i = 0; i < arr.length; i++) {
            Node node = new Node();
            node.index = i;
            node.color = colorArr[i];
            node.value = arr[i];
            rsuMap.put(node,node);
            nodes[i] = node;
        }
        for (int i = 0; i < arr.length; i++) {
            for (int j = i+1; j < arr.length; j++) {
               if(i!=j &&(arr[i] == arr[j] || colorArr[i] == colorArr[j])){
                   Node parentI = findRoot(rsuMap,nodes[i]);
                   Node parentJ = findRoot(rsuMap,nodes[j]);
                   if(parentI != parentJ){
                        rsuMap.put(parentJ,parentI);
                    }
               }
            }
        }
//        for (int i = 0; i < arr.length; i++) {
//            for (int j = i+1; j < arr.length; j++) {
//                if(i!=j &&(colorArr[i] == colorArr[j])){
//                    Node parentI = findRoot(rsuMap,nodes[i]);
//                    Node parentJ = findRoot(rsuMap,nodes[j]);
//                    if(parentI != parentJ){
//                        rsuMap.put(parentJ,parentI);
//                    }
//                }
//            }
//        }
        int[] countArr = new int[nodes.length];
        for (int i = 0; i < nodes.length; i++) {
            countArr[findRoot(rsuMap,nodes[i]).index]++;
        }
        System.out.println(countArr);
    }

    public static Node findRoot(Map<Node,Node> rsuMap,Node curNode){
        if(rsuMap.get(curNode) != curNode){
            return findRoot(rsuMap,rsuMap.get(curNode));
        }
        return curNode;
    }
}
